//
//  ContactsViewController.m
//  QQ界面
//
//  Created by apple on 16/10/22.
//  Copyright © 2016年 LWB. All rights reserved.
//

#import "ContactsViewController.h"
#import "AddViewController.h"
#import "FriendGroup.h"
#import "GroupHeaderView.h"
#import "FriendCell.h"
#import "ChatViewController.h"
#import "DropDownRefreshView.h"
@interface ContactsViewController ()<UITableViewDelegate,UITableViewDataSource,GroupHeaderViewDelegate>
@property (nonatomic ,strong)UIView *ctView;
@property (nonatomic,strong)UITableView *tbView;
//分组模型的数组
@property(nonatomic, strong)NSArray * groups;

@end

@implementation ContactsViewController
#pragma mark -实现headerView的协议方法
-(void)groupHeaderViewClieckTitleButton:(GroupHeaderView *)groupHeaderView{
    NSIndexSet *idxSet = [NSIndexSet indexSetWithIndex:groupHeaderView.tag];
    NSLog(@"%ld",(long)groupHeaderView.tag);
    [_tbView reloadSections:idxSet withRowAnimation:UITableViewRowAnimationFade];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationItemControls];
    [self setUpViewAddControls];
    [self groups];
    UITextField *text = [[UITextField alloc]init];
    
    self.automaticallyAdjustsScrollViewInsets =NO;
    _tbView =[[UITableView alloc]initWithFrame:CGRectMake(0, 29, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-58) style:UITableViewStyleGrouped];
    _tbView.delegate = self;
    _tbView.dataSource =self;
    _tbView.sectionFooterHeight = 0;
//    _tbView.bottomAnchor
    _tbView.layer.masksToBounds=NO;
    DropDownRefreshView *dropDown = [[DropDownRefreshView alloc]initWithFrame:CGRectMake(0,-29 , self.view.bounds.size.width,64)];
 //  dropDown.backgroundColor =[UIColor whiteColor];
    [self.tbView addSubview:dropDown];
    [self.view addSubview:_tbView];
    // Do any additional setup after loading the view.
}
-(void)setUpViewAddControls{
    self.ctView = [[UIView alloc]initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, 140)];
    //  [self.view addSubview:_norView];
    [self setThreeButton];
    [self setSearchBar];
}
-(void)setThreeButton{
    UIButton *newFriendBut = [[UIButton alloc]initWithFrame:CGRectMake(0, 60, [UIScreen mainScreen].bounds.size.width/3, 80)];
    [newFriendBut setBackgroundImage:[UIImage imageNamed:@"newFriend-p"] forState:UIControlStateNormal];
    [newFriendBut addTarget:self action:@selector(newFriendAction) forControlEvents:UIControlEventTouchUpInside];
    [self.ctView addSubview:newFriendBut];
    UIButton *groupChatBut = [[UIButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/3,60, [UIScreen mainScreen].bounds.size.width/3, 80)];
    [groupChatBut setBackgroundImage:[UIImage imageNamed:@"ngroupChat-p"] forState:UIControlStateNormal];
    [groupChatBut addTarget:self action:@selector(groupChatAction) forControlEvents:UIControlEventTouchUpInside];
    [self.ctView addSubview:groupChatBut];
    UIButton *publicBut = [[UIButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/3*2,60, [UIScreen mainScreen].bounds.size.width/3, 80)];
    [publicBut setBackgroundImage:[UIImage imageNamed:@"public-p"] forState:UIControlStateNormal];
    [publicBut addTarget:self action:@selector(publicButAction) forControlEvents:UIControlEventTouchUpInside];
    [self.ctView addSubview:publicBut];
}
-(void)newFriendAction{
    
}
-(void)groupChatAction{
    
}
-(void)publicButAction{
    
}
-(void)setSearchBar{
    UISearchBar *searBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, 60)];
    searBar.backgroundImage = [UIImage imageNamed:@"UISearchBar_p"];
    searBar.searchBarStyle = UISearchBarStyleMinimal ;
    searBar.placeholder = @"搜索";
    [self.ctView addSubview:searBar];
}
-(void)setNavigationItemControls{
    self.navigationItem.title = @"联系人";
    self.view.backgroundColor = [UIColor greenColor];
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(addAction)];
    self.navigationItem.rightBarButtonItem = rightBarButton;
}
-(void)addAction{
    AddViewController *addVi = [[AddViewController alloc]init];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:addVi animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
#pragma mark -Table代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 1;
    }else{
        FriendGroup * group = _groups[section-1];
        //  return group.friends.count;
//        //   判断该组目前是展开状态还是收起状态
        if (group.isVisible) {
            return group.friends.count;
        }else{
            return 0;
        }
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  //  indexPath.section
    //创建要显示的聊天界面视图控制器
    ChatViewController * chat = [[ChatViewController alloc]init];
  //  NSLog(@"%ld",(long)[chat.indPath section]);
    //创建一个属于chat、的导航栏
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:chat];
    //设置导航栏的标题
   // chat.title = [_plistArr[indexPath.row] objectForKey:@"name"];
    //跳转到这个导航栏
//    [self presentViewController:nav animated:YES completion:nil];
    int i =indexPath.section;
    int y = indexPath.row;
//    [[NSNotificationCenter alloc]addObserver:chat selector:@selector(Action:) name:@"chat" object:nil];
//    NSNotification *cation= [NSNotification notificationWithName:@"chat" object:self userInfo:@{@"section":[NSNumber numberWithInt:i],@"row":[NSNumber numberWithInt:y]}];
//     [[NSNotificationCenter defaultCenter] postNotification:cation];
    //创建一个消息对象
//    [NSTimer scheduledTimerWithTimeInterval:0.22 target:self selector:@selector(shed) userInfo:nil repeats:NO];
    //创建一个消息对象
    NSNotification * notice = [NSNotification notificationWithName:@"123" object:nil userInfo:@{@"1":@"123"}];
    //发送消息
    [[NSNotificationCenter defaultCenter]postNotification:notice];
    self.hidesBottomBarWhenPushed = YES;
    [self presentViewController:nav animated:YES completion:nil];
    //  chat.indexPath indexPath
//    chat.section = indexPath.section;
//    chat.row = indexPath.row;
//     chat.str = @"111";
}
-(void)shed{
    //创建一个消息对象
    NSNotification * notice = [NSNotification notificationWithName:@"123" object:nil userInfo:@{@"1":@"123"}];
    //发送消息
    [[NSNotificationCenter defaultCenter]postNotification:notice];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     return _groups.count+1;// 好友一共有分组模型对象数组的数组大小的组
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FriendCell * cell = [[FriendCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    if ([indexPath section]==0) {
        [cell.contentView addSubview:_ctView];
    }else{
        //分组模型
        FriendGroup * group = _groups[indexPath.section-1];
        //好友模型
        Friend * friend = group.friends[indexPath.row];
        cell.friendModel = friend;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([indexPath section]==0) {
        return 140;
    }
    return 45;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 35;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section!=0&&section!=2) {
        return 0;
    }
    return 15;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section!=0) {
    GroupHeaderView *headVw= [[GroupHeaderView alloc]init];
        headVw.group = _groups[section-1];
        headVw.tag = section;
        headVw.delegatep = self;
        return headVw;
    }
    return nil;
}
#pragma mark - 懒加载分组模型数据
-(NSArray*)groups
{
    if (_groups == nil) {
        //加载,不为空就不加载
        //1.获取文件路径
        NSString * path = [[NSBundle mainBundle]pathForResource:@"friends.plist" ofType:nil];
        //2.读取文件内容到一个数组中
        NSArray * arrayDicts = [NSArray arrayWithContentsOfFile:path];
        //3.创建一个可变数组来保存分组模型对象
        NSMutableArray * arrayModels = [NSMutableArray array];
        //4.将从plist文件中读取出来的分组模型对象添加到可变数组中
        for (NSDictionary * dict in arrayDicts) {
            //创建分组模型对象来接收数据
            FriendGroup * model = [FriendGroup friendGroupWithDict:dict];
            [arrayModels addObject:model];
        }
        //5.将保存好的分组模型数组赋给属性
        _groups = arrayModels;
    }
    return _groups;
}
//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    return @"             ";
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
